<?php
/**
 * 好友
 * 
 * Created	: 2011-05-18
 * Modified : 2011-05-18
 * @author  : 陈立强
 */
class Friend
{
	
	/**
	 * 获取玩家信息
	 * @param int $uid
	 * @param int $target_uid
	 */
	public static function getInfo($uid, $target_uid)
	{
		global $DB, $L, $G;
		
		$sql = 'SELECT * FROM user WHERE uid='.$target_uid; 
		$info = $DB->getValue($sql);
		
		return $info;
	}
	
	/**
	 * 根据玩家昵称取信息
	 * @param int $uid
	 * @param int $nickname
	 */
	public static function getInfoByName($uid, $nickname)
	{
		global $DB, $L, $G;
		
		$sql = 'SELECT * FROM user WHERE nickname=:nickname';
		$param = array('nickname'=>$nickname);
		$info = $DB->getValue($sql, $param);
		
		return $info;
	}
	
	/**
	 * 获取好友友好度
	 * @param int $uid
	 * @param int $friend_id
	 */
	public static function getFriendlyVal($uid, $friend_id)
	{
		global $DB, $L, $G;
		
		$friend_id = (int)$friend_id;
		$where = '(uid='.$uid.' AND to_uid='.$friend_id.') OR (uid='.$friend_id.' AND to_uid='.$uid.')';
		$sql = 'SELECT friend_val FROM user_friend WHERE '.$where; 
		$info = (int)$DB->getValue($sql);
		
		return $info;
	}
	
	/**
	 * 和对方是否好友
	 * @param int $uid
	 * @param int $friend_id
	 */
	public static function isFriend($uid, $friend_id)
	{
		global $DB, $L, $G;
		
		$friend_id = (int)$friend_id;
		$where = '(uid='.$uid.' AND to_uid='.$friend_id.') OR (uid='.$friend_id.' AND to_uid='.$uid.')';
		$sql = 'SELECT uf_id FROM user_friend WHERE '.$where; 
		$uf_id = $DB->getValue($sql);
		
		return $uf_id;
		
	}
		
	/**
	 * 添加好友
	 * @param int $uid 当前玩家ID
	 * @param int|string $friend 添加为好友的目标玩家ID/昵称
	 */
	public static function add($uid, $friend)
	{
		global $DB, $L, $G;
		
		if (is_int_string($friend)) {
			$friend_id = (int)$friend;
		} else {
			$friend_id = User::readByNickname($friend, 'uid');
			if (!$friend_id) {
				return array(
					'val' => false,
					'msg' => $L['user_not_exists']
				);
			}
		}
		
		$where = '(uid='.$uid.' AND to_uid='.$friend_id.') OR (uid='.$friend_id.' AND to_uid='.$uid.')';
		$sql = 'SELECT uf_id,status FROM user_friend WHERE '.$where;
		$friend = $DB->getValue($sql);
		if ($friend) {
			return array(
				'val' => false,
				'msg' => $L['user_had_your_friend']
			);
		}
		
		$DB->beginTransaction();
		
		if ($friend) {
			$data = array(
				'status' => 0
			);
			$ret = $DB->update('user_friend', $data);
		} else {
			$data = array(
				'uid' => $uid,
				'to_uid' => $friend_id,
				'status' => 0,
			);
			$ret = $DB->insert('user_friend', $data);
		}
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
			$DB->commit();
		} else {
			$DB->rollBack();
			$result['msg'] = $L['Failure'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('friend_add', $str);
		}

		return $result;
	}
	
	/**
	 * 确认添加好友
	 * @param int $uid 当前玩家ID
	 * @param int $fromuid 请求添加好友玩家ID
	 */
	public static function confirm($uid, $fromuid)
	{
		global $DB, $L, $G;
		$sql = 'SELECT uf_id,status FROM user_friend WHERE uid='.$fromuid.' and to_uid='.(int)$uid;
		$friend = $DB->getValue($sql);
		$friendState = $DB->getValue($sql);
		// 无效的好友请求
		if (!$friend) {
			return array(
				'val' => false,
				'msg' => $L['user_no_require_friend']
			);
		}
		// 已经是好友了
		if ($friend['status']==1) {
			return array(
				'val' => false,
				'msg' => $L['user_had_your_friend']
			);
		}
		
		$data = array(
			'status' => 1
		);
		$where = 'uf_id='.$friend['uf_id'];
		$ret = $DB->update('user_friend', $data, $where);
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
		} else {
			$result['msg'] = $L['Failure_Update'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('friend_confirm', $str);
		}

		return $result;
	}
	
	/**
	 * 删除好友
	 * @param int $uid
	 * @param int $fuid
	 */
	public static function delete($uid, $fuid)
	{
		global $DB, $L, $G;
		$where = '(uid='.$uid.' and to_uid='.$fuid
					.') or (uid='.$fuid.' and to_uid='.$uid.')';
		$sql = 'SELECT * FROM user_friend WHERE '.$where;
		$uf = $DB->getValue($sql);
		if (!$uf) {// 非好友不能删除
			return array(
				'val' => false,
				'msg' => $L['user_isnot_friend']
			);
		}
		
		$where = 'uf_id='.$uf['uf_id'];
		$ret = $DB->delete('user_friend', $where);
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
		} else {
			$result['msg'] = $L['Failure_Update'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('friend_delete', $str);
		}

		return $result;
	}
	
	/**
	 * 获取好友列表
	 * @param int $uid
	 * @param int|null $status 参数:null-所有好友(含未确认),0-未确认好友,1-已确认好友
	 */
	public static function getList($uid, $status=null)
	{
		global $DB, $L, $G;
		
		$today = strtotime('today');
		
		
		// 取好友ID列表
		if (is_null($status)) {
			$where = 'uid='.$uid.' or to_uid='.$uid.'';
		} else {
			$where = '(uid='.$uid.' or to_uid='.$uid.') and status='.(int)$status;
		}
		$sql = 'SELECT uid,to_uid,friend_val,present_time,to_present_time,status FROM user_friend WHERE '.$where;
		$list = $DB->getList($sql);
		if (!$list) {
			return array();
		}
		$fuidList = array();
		$relationList = array();
		foreach ($list as $v) {
			if ($v['uid'] == $uid) {
				$fuidList[] = $v['to_uid'];
				if ($v['status'] == 1) {
					$relationList[$v['to_uid']] = array(
						'status'		=> 1,
						'friend_val'	=> $v['friend_val'],
						'present_time'	=> $v['present_time'],
					);
				} else {
					$relationList[$v['to_uid']] = array(
						'status'		=> 0,
						'friend_val'	=> $v['friend_val'],
						'present_time'	=> $v['present_time'],
					);
				}
			} else {
				$fuidList[] = $v['uid'];
				if ($v['status'] == 1) {
					$relationList[$v['uid']] = array(
						'status'		=> 1,
						'friend_val'	=> $v['friend_val'],
						'present_time'	=> $v['to_present_time'],
					);
				} else {
					$relationList[$v['uid']] = array(
						'status'		=> 10,
						'friend_val'	=> $v['friend_val'],
						'present_time'	=> $v['to_present_time'],
					);
				}
			}
		}
		
		// 由好友ID列表取好友信息列表 
		$uidStr = join(',', $fuidList);
		$sql = 'SELECT uid,nickname,sex,last_online,is_online FROM user WHERE uid in ('.$uidStr.')';
		$list = $DB->getList($sql);
		$now = time();
		foreach ($list as $k=>$v) {
			if (isset($relationList[$v['uid']])) {
				$list[$k]['self_confirm']	= (bool)($relationList[$v['uid']]['status']==10);
				$list[$k]['other_confirm']	= (bool)($relationList[$v['uid']]['status']==0);
				$list[$k]['status']			= $relationList[$v['uid']]['status'];
				$list[$k]['friend_val']		= $relationList[$v['uid']]['friend_val'];
				$list[$k]['present_time']	= $relationList[$v['uid']]['present_time'];
				if ($relationList[$v['uid']]['present_time'] >= $today) {
					$list[$k]['presented_today'] = true;
				} else {
					$list[$k]['presented_today'] = false;
				}
			}
			if ($v['is_online']) {
				if ((!$v['last_online'] || ($now - $v['last_online']) >= $G['session_time'])) {
					$list[$k]['is_online'] = 0;
				}
			}
		}
		
		return $list;
	}
}










